package com.aliwo.timeExample;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * .
 *
 * @author Xu Yayuan
 * @date 2023-05-11
 */
public class LocalDateTimeTest {

    private static ConcurrentMap<String, Object> httpClientMap = new ConcurrentHashMap<>();

    public static void main(String[] args) {
        // 当前时间
        LocalDateTime dateTime = LocalDateTime.now();
        //当前时间两个小时前
        LocalDateTime beforeTime = dateTime.minus(2, ChronoUnit.HOURS);
        System.out.println("beforeTime:" + beforeTime);
        System.out.println("dateTime:" + dateTime);
        httpClientMap.putIfAbsent("lastLoginTime", LocalDateTime.now().minus(5, ChronoUnit.HOURS));
        // 获取两个时间之间的Duration对象
        Duration duration = Duration.between((Temporal) httpClientMap.get("lastLoginTime"), LocalDateTime.now());
        long hours = duration.toHours(); // 将Duration转为小时数
        if (hours > 12) {
            System.out.println("两者时间相差hours:" + hours);
        }
        System.out.println("两者时间相差hours:" + hours);

        // 当天凌晨零点零分
        LocalDateTime nowDay = LocalDate.now().atStartOfDay();
        // 前一天天凌晨零点零分
        LocalDateTime beforeOneDay = LocalDate.now().minusDays(1).atStartOfDay();
        System.out.println("当天凌晨零点零分 nowDay:" + nowDay);
        System.out.println("前一天天凌晨零点零分 beforeOneDay:" + beforeOneDay);
    }
}
